#include "MediumRubble.h"
#include <GL/glut.h>

// Makes the rubble part colored
GLfloat mr_diffuse[] = {0.20, 0.20, 0.20, 1.0};

MediumRubble::MediumRubble(void)
{	
	// For the team number
	teamNumberSizeF = 0.4;
	teamNumberSizeS = 0.1;
	loadTextureImages();
}


MediumRubble::~MediumRubble(void)
{
}

void MediumRubble::loadTextureImages()
{
	// load texture images
	Image* image2 = loadBMP("textures\\rustymetal.bmp");
	tileTexture = loadTexture(image2);
	textureFile = "textures\\Scrapmetal.bmp";
    imageSkyBox = loadBMP(textureFile.c_str());
    _textureId = loadTexture(imageSkyBox);
    delete imageSkyBox;
}

void MediumRubble::drawCube(void) {
	glTranslatef(-0.5, -0.5, -0.5);
	// vertices
	GLfloat A[] = { 0.0f, 1.0f, 1.0f }; 
	GLfloat B[] = { 1.0f, 1.0f, 1.0f }; 
	GLfloat C[] = { 1.0f, 1.0f, 0.0f }; 
	GLfloat D[] = { 0.0f, 1.0f, 0.0f }; 
	GLfloat E[] = { 0.0f, 0.0f, 1.0f }; 
	GLfloat F[] = { 1.0f, 0.0f, 1.0f }; 
	GLfloat G[] = { 1.0f, 0.0f, 0.0f }; 
	GLfloat H[] = { 0.0f, 0.0f, 0.0f }; 

	// texture coordinates
	GLfloat bottomLeft[] = {0.0f, 0.0f};
	GLfloat bottomRight[] = {1.0f, 0.0f};
	GLfloat topLeft[] = {0.0f, 1.0f};
	GLfloat topRight[] = {1.0f, 1.0f};

	//glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, tileTexture);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	// top
	glBegin(GL_QUADS);
		glNormal3f( 0.0, 1.0, 0.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(A);
		glTexCoord2fv(bottomRight);
		glVertex3fv(B);
		glTexCoord2fv(topRight);
		glVertex3fv(C);
		glTexCoord2fv(topLeft);
		glVertex3fv(D);
	glEnd();

	// bottom
	glBegin(GL_QUADS);
		glNormal3f( 0.0, -1.0, 0.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(H);
		glTexCoord2fv(bottomRight);
		glVertex3fv(G);
		glTexCoord2fv(topRight);
		glVertex3fv(F);
		glTexCoord2fv(topLeft);
		glVertex3fv(E);
	glEnd();

	// Front
	glBegin(GL_QUADS);
		glNormal3f( 0.0, 0.0, 1.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(E);
		glTexCoord2fv(bottomRight);
		glVertex3fv(F);
		glTexCoord2fv(topRight);
		glVertex3fv(B);
		glTexCoord2fv(topLeft);
		glVertex3fv(A);
	glEnd();

	// back
	glBegin(GL_QUADS);
		glNormal3f( 0.0, 0.0, -1.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(G);
		glTexCoord2fv(bottomRight);
		glVertex3fv(H);
		glTexCoord2fv(topRight);
		glVertex3fv(D);
		glTexCoord2fv(topLeft);
		glVertex3fv(C);
	glEnd();

	// left
	glBegin(GL_QUADS);
		glNormal3f( -1.0, 0.0, 0.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(H);
		glTexCoord2fv(bottomRight);
		glVertex3fv(E);
		glTexCoord2fv(topRight);
		glVertex3fv(A);
		glTexCoord2fv(topLeft);
		glVertex3fv(D);
	glEnd();

	// right
	glBegin(GL_QUADS);
		glNormal3f( 1.0, 0.0, 0.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(F);
		glTexCoord2fv(bottomRight);
		glVertex3fv(G);
		glTexCoord2fv(topRight);
		glVertex3fv(C);
		glTexCoord2fv(topLeft);
		glVertex3fv(B);
	glEnd();
}


void MediumRubble::display(int up, int left)
{
	glPushMatrix();
		glRotatef((GLfloat) left, 0.0, 1.0, 0.0);
		glRotatef((GLfloat) up, 1.0, 0.0, 0.0);
		glColor3f(1.0,1.0,0);

		// Produces the tile
		glPushMatrix();
			glBindTexture(GL_TEXTURE_2D, tileTexture);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glScalef(1.0, 1.0, 1.0);
			drawCube();
		glPopMatrix();

		GLUquadricObj* quad2 = gluNewQuadric();

		// Produces medium rubble
		glPushMatrix();
			glMaterialfv(GL_FRONT, GL_DIFFUSE, mr_diffuse);

			gluQuadricNormals(quad2, GLU_SMOOTH);   // Create Smooth Normals (better for light)
			gluQuadricTexture(quad2, GL_TRUE);      // Create Texture Coords

			glBindTexture(GL_TEXTURE_2D, _textureId);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

			glRotatef(-90.0, 1.0, 0.0, 0.0);
			glTranslatef(0.3, 0.3, 0.5);
			glColor3f(0.8, 0.8, 0.0);
			gluCylinder(quad2, 0.2, 0.2, 0.1, 20, 20);
			glTranslatef(0.0, 0.0, 0.1);
			gluCylinder(quad2, 0.2, 0.0, 0.3, 20, 20);
		glPopMatrix();

		// Places cones on the tile to simulate rubble
		glPushMatrix();
			gluQuadricNormals(quad2, GLU_SMOOTH);   // Create Smooth Normals (better for light)
			gluQuadricTexture(quad2, GL_TRUE);      // Create Texture Coords

			glBindTexture(GL_TEXTURE_2D, _textureId);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

			glRotatef(-90.0, 1.0, 0.0, 0.0);
			glTranslatef(-0.3, 0.3, 0.5);
			glColor3f(0.8, 0.8, 0.0);
			gluCylinder(quad2, 0.2, 0.2, 0.1, 20, 20);
			glTranslatef(0.0, 0.0, 0.1);
			gluCylinder(quad2, 0.2, 0.0, 0.3, 20, 20);
		glPopMatrix();

		glPushMatrix();
			gluQuadricNormals(quad2, GLU_SMOOTH);   // Create Smooth Normals (better for light)
			gluQuadricTexture(quad2, GL_TRUE);      // Create Texture Coords

			glBindTexture(GL_TEXTURE_2D, _textureId);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

			glRotatef(-90.0, 1.0, 0.0, 0.0);
			glTranslatef(0.3, -0.3, 0.5);
			glColor3f(0.8, 0.8, 0.0);
			gluCylinder(quad2, 0.2, 0.2, 0.1, 20, 20);
			glTranslatef(0.0, 0.0, 0.1);
			gluCylinder(quad2, 0.2, 0.0, 0.3, 20, 20);
		glPopMatrix();

		glPushMatrix();
			gluQuadricNormals(quad2, GLU_SMOOTH);   // Create Smooth Normals (better for light)
			gluQuadricTexture(quad2, GL_TRUE);      // Create Texture Coords

			glBindTexture(GL_TEXTURE_2D, _textureId);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

			glRotatef(-90.0, 1.0, 0.0, 0.0);
			glTranslatef(-0.3, -0.3, 0.5);
			glColor3f(0.8, 0.8, 0.0);
			gluCylinder(quad2, 0.2, 0.2, 0.1, 20, 20);
			glTranslatef(0.0, 0.0, 0.1);
			gluCylinder(quad2, 0.2, 0.0, 0.3, 20, 20);
		glPopMatrix();

		glPushMatrix();
			gluQuadricNormals(quad2, GLU_SMOOTH);   // Create Smooth Normals (better for light)
			gluQuadricTexture(quad2, GL_TRUE);      // Create Texture Coords

			glBindTexture(GL_TEXTURE_2D, _textureId);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

			glRotatef(-90.0, 1.0, 0.0, 0.0);
			glTranslatef(-0.0, -0.0, 0.5);
			glColor3f(0.8, 0.8, 0.0);
			gluCylinder(quad2, 0.4, 0.2, 0.1, 20, 20);
			glTranslatef(0.0, 0.0, 0.1);
			gluCylinder(quad2, 0.4, 0.0, 0.4, 20, 20);
			glutSolidCone(0.4, 0.40, 20, 20);
		glPopMatrix();

		// Produces team number
		/*glPushMatrix();
			glTranslatef(teamNumberSizeF/3, 0.025 + teamNumberSizeS/2, 0.0);
			glPushMatrix();
				glTranslatef(-teamNumberSizeF/3, 0.0, -teamNumberSizeF/8);
				glScalef(2*teamNumberSizeF/3, 0.05, teamNumberSizeF/4);
				glColor3f(0, 1, 0);
				glutSolidCube(1.0);
			glPopMatrix();
			glPushMatrix();
				glRotatef(-25, 0.0, 1.0, 0.0);
				glTranslatef(-teamNumberSizeF/8, 0.0, teamNumberSizeF/2);
				glScalef(teamNumberSizeF/4, 0.05, teamNumberSizeF);
				glColor3f(0, 1, 0);
				glutSolidCube(1.0);
			glPopMatrix();
		glPopMatrix();*/
	glPopMatrix();
}